const express = require('express');
const path = require('path');
const compass = require('node-compass');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
require('./db/init')
// Policies:
const accessTokenMiddleware = require('./policies/accessToken.policy');
const vipsMiddleware = require('./policies/middleware.vip');
const refreshTokenMiddleware = require('#policies/refreshToken.policy');
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const tcRouter = require('./routes/tc');
const costRouter = require('./routes/cost');
const holdersRouter = require('./routes/holder');
const baseRouter = require('./routes/base');

const app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(compass({mode: 'expanded'}));
app.use(express.static(path.join(__dirname, 'public')));
// app.all('/api/v1/users', function (req, res, next) {
//     console.log("/api/v1/")
//     next()
// })
const priPath = '/api/v1'
const openApiPath = '/openapi/v1'
app.all(`${priPath}/*`, accessTokenMiddleware);
app.use('/', indexRouter);
app.use(`${openApiPath}/users`, usersRouter);
// app.use('/api/v1/users/login', usersRouter);
app.use(`${openApiPath}/tc`, tcRouter);
app.use(`${priPath}/cost`, costRouter);
app.use(`${openApiPath}/holders`, holdersRouter);
app.use(`${openApiPath}/base`, baseRouter);
app.use(`${openApiPath}/base/vip1`, vipsMiddleware,baseRouter);

module.exports = app;
